package com.xcc.common;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @outhor Ren
 * @create 2019-03-21 22:54
 * 描述:
 * 获取上下文工具
 */
@Component("applicationContextHelper")
public class ApplicationContextHelper implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public static <T> T popBean(Class<T> cls) {
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(cls);
    }

    public static <T> T popBean(String name, Class<T> cls) {
        if (applicationContext == null) {
            return null;
        }
        return applicationContext.getBean(name, cls);
    }
}
